package cn.prophet.controller;

import cn.prophet.bean.BankCard;
import cn.prophet.bean.vo.AliBankBean;
import cn.prophet.bean.vo.SuperResponseListBean;
import cn.prophet.service.BankCardService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("bankcard")
public class BankCardController {
    @Resource
    private BankCardService bankCardService;
    @GetMapping("addcard")
    @ResponseBody
    public Map<String,Object> addBankCard(@RequestParam(value = "userId",required = true) Long userId, @RequestParam(value = "bank_username",required = true) String userName, @RequestParam(value = "bank_num",required = true) String bankNum, @RequestParam(value = "bank_name",required = true)String bankName){

        Map<String,Object> map = new HashMap<>();
        if(StringUtils.isEmpty(userName)||StringUtils.isEmpty(bankName)||StringUtils.isEmpty(bankNum)){
            map.put("code",0);
            map.put("msg","请正确填写信息");
            return map;
        }
        int result = bankCardService.addBankCard(userId,userName,bankNum,bankName);

        if(result==2){
            map.put("code",0);
            map.put("msg","用户不存在");
        }else if(result==1){
            map.put("code",1);
            map.put("msg","绑卡成功");
        }else if(result==3){
            map.put("code",0);
            map.put("msg","只能绑定一张银行卡");
        }else{
            map.put("code",0);
            map.put("msg","绑卡失败");
        }

        return map;
    }
    @GetMapping("getBankName")
    @ResponseBody
    public AliBankBean getBankName(@RequestParam(value = "bank_num",required = true)String bankNum){

        AliBankBean aliBankBean  =bankCardService.getBankName(bankNum);

        return aliBankBean;
    }
    @GetMapping("getBankCard")
    @ResponseBody
    public SuperResponseListBean<BankCard> getBankCard(@RequestParam(value = "userId",required = true) Long userId){
            SuperResponseListBean<BankCard> listBean = new SuperResponseListBean<>();
            List<BankCard> bankCardList = bankCardService.getBankCard(userId);
            if(bankCardList!=null&&bankCardList.size()>0){
                listBean.setCode(1);
                listBean.setMsg("查询成功");
            }else{
                listBean.setCode(0);
                listBean.setMsg("暂无数据");
            }
            listBean.setData(bankCardList);

        return listBean;

    }
    @GetMapping("releaseBankCard")
    @ResponseBody
    public Map<String,Object> releaseBankCard(@RequestParam(value = "cardId",required = true) Long cardId){
        int result = bankCardService.releaseBankCard(cardId);
        Map<String,Object> map = new HashMap<>();
        if(result==1){
            map.put("code",1);
            map.put("msg","解除成功");
        }else if(result==2){
            map.put("code",0);
            map.put("msg","没绑定该银行卡");
        } else if(result==3){
            map.put("code",0);
            map.put("msg","该银行卡已解除绑定");
        } else{
            map.put("code",0);
            map.put("msg","解除失败");
        }
        return map;

    }
}
